package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.Doctor;
import com.entity.Patient;
import com.mapper.DoctorMapper;
import com.mapper.PatientMapper;
import com.service.DoctorService;
import com.service.PatientService;
import com.untils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    @Autowired
    DoctorMapper doctorMapper;

    @Override
    public String login_doctor(Doctor doctor){
        System.out.println("进入login_doctor");
        List<Doctor> selectedList = list(new LambdaQueryWrapper<Doctor>()
                .eq(Doctor::getDoctorId, doctor.getDoctorId()));
        if (selectedList.isEmpty()) {
            throw new RuntimeException("账号不存在哦 请先注册吧~");
        }
        Doctor selected = selectedList.get(0);
        String encodedPassword = selected.getPsw();
        if (!doctor.getPsw().equals(encodedPassword)) {
            throw new RuntimeException("账号或密码有误 请再检查检查~");
        }
        // 生成令牌
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("DoctorId", selected.getDoctorId());
        map.put("psw", selected.getPsw());
        System.out.println("返回令牌，令牌为：" + JwtUtil.generateToken(map));
        return JwtUtil.generateToken(map);
    }
}
